package com.bs.resa.common;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bs.resa.shiro.ShiroUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author lin
 * @date 2019/5/1 17:45
 */
public class MyExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView model=new ModelAndView();
        System.out.println("没有权限");
        //是ajax请求
        if(ShiroUtils.isAjax(httpServletRequest)){
            System.out.println("ajax请求");
            Map<String,String> map=new HashMap<>();
            map.put("code","403");
            ShiroUtils.out(httpServletResponse, map);
            return  null;
        }else{
            System.out.println("url跳转");
            model.addObject("message",e.getMessage());
            model.setViewName("403");
            return model;

        }
    }
}
